package com.topboya.linker.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.Button;

import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.activity.SeeSelectedFileActivity;
import com.topboya.linker.ui.activity.xSeeFileActivity;
import com.topboya.linker.ui.activity.xSelectFileActivity;
import com.topboya.linker.ui.adapter.SeeFileAdapter;
import com.topboya.linker.ui.adapter.SelectFileAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.base.fragment.BaseFragment;
import com.topboya.linker.ui.presenter.SelectFilePresenter;
import com.topboya.linker.ui.view.SelectFileView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class SeeFileClassifyFragment extends BaseFragment<SelectFileView, SelectFilePresenter> implements SelectFileView {

    private static final String TAG = "FileClassifyFragment-";
    private String fileLabelvalues;
    private RecyclerView recycle_display_file;
    private String productId;
    private Toolbar toolbar;
    //    宿主activity里面的button
    private Button btn_selected;
    private Button btn_select;
    private List<SelectFileResponse.FileList> fileLists;
    private SeeFileAdapter seeFileAdapter;
    private boolean isInitView = false;
    private boolean isVisible = false;
    private xSeeFileActivity xseeFileActivity;
//    点击产品条目的位置
    private String position;

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_select_file;
    }

    @Override
    protected void initView() {

        LogUtils.v(TAG + "initView()");
        recycle_display_file = mRootView.findViewById(R.id.recycle_display_file);
        toolbar = getActivity().findViewById(R.id.toolbar);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        recycle_display_file.setLayoutManager(layoutManager);
        fileLists = new ArrayList<>();
        seeFileAdapter = new SeeFileAdapter(mContext, (ArrayList<SelectFileResponse.FileList>) fileLists);
        recycle_display_file.setAdapter(seeFileAdapter);
        xseeFileActivity = (xSeeFileActivity) getActivity();
    }

    @Override
    protected void initData() {

       LogUtils.v("initData()");
        productId = getActivity().getIntent().getStringExtra("productId");
        position = getActivity().getIntent().getStringExtra("position");
        LogUtils.v(TAG + "position:"+position);
        fileLabelvalues = getArguments().getString("fileLabelvalues");
        LogUtils.v(TAG + "productId:" + productId);
        LogUtils.v(TAG + "initData--fileLabelvalues:" + fileLabelvalues);
        isInitView = true;
        isCanLoadData();
//        lazyLoad();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
         LogUtils.v(TAG + "setUserVisibleHint:"+isVisibleToUser);
        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见，获取该标志记录下来
        if (isVisibleToUser) {

            isVisible = true;
            isCanLoadData();
        } else {

            isVisible = false;
        }
    }

    private void isCanLoadData() {

        //所以条件是view初始化完成并且对用户可见
        LogUtils.v(TAG + "isInitView:"+isInitView+"isVisible:"+isVisible);
        if (isInitView && isVisible) {
            lazyLoad();
            //防止重复加载数据
            isInitView = false;
            isVisible = false;
        }
    }

    private void lazyLoad() {

        LogUtils.v(TAG + "lazyLoad--fileLabelvalues:" + fileLabelvalues);

        mPresenter.getProductFileList(productId, fileLabelvalues);

    }

    @Override
    public void initListener() {

        LogUtils.v(TAG + "initListener()");
        toolbar.setNavigationOnClickListener(v -> xseeFileActivity.finish());
        seeFileAdapter.setOnItemClickListener((v, position) -> {

        });

        xseeFileActivity.setonPagerChangedListener(position -> isInitView =true );
        xseeFileActivity.setKeyBackListener(() -> xseeFileActivity.finish());
        xseeFileActivity.setAdapterNotifyListener(() -> {
            UIUtils.showToast("刷新");
            seeFileAdapter.notifyDataSetChanged();
        });
    }



    @Override
    protected SelectFilePresenter createPresenter() {

        return new SelectFilePresenter((BaseActivity) getActivity());
    }


    @Override
    public void getProductFileList(List<SelectFileResponse.FileList> fileLists) {
        LogUtils.v(TAG + "getProductFileList()");
        this.fileLists.clear();
        this.fileLists.addAll(fileLists);
        seeFileAdapter.notifyDataSetChanged();
    }



}
